package com.hly.musicbackground.model.boots.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Final.FictionFinal;
import com.hly.musicbackground.Final.RedisFinal;
import com.hly.musicbackground.Final.SearchFinal;
import com.hly.musicbackground.Utils.JsoupBooksUtil;
import com.hly.musicbackground.Utils.RedisUtil;
import com.hly.musicbackground.Utils.StringUtils;
import com.hly.musicbackground.dao.FictionContentDao;
import com.hly.musicbackground.dao.FictionDao;
import com.hly.musicbackground.domain.FictionChapter;
import com.hly.musicbackground.domain.FictionContent;
import com.hly.musicbackground.model.boots.VO.FictionContentVO;
import com.hly.musicbackground.model.boots.service.FictionContentService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FictionContentServiceImpl extends ServiceImpl<FictionContentDao, FictionContent> implements FictionContentService {

    private final RedisUtil redisUtil;

    private final FictionDao fictionDao;

    private final FictionDocumentWwwFyhuaboCom fictionDocument;

    @Override
    public FictionContentVO getFictionContent(FictionChapter fictionChapter) {
        FictionContentVO fictionContentVO = new FictionContentVO();

        if (StringUtils.isEmpty(fictionChapter.getChapterLink())){
            fictionDao.updateAbnormal(fictionChapter.getFictionId(), FictionFinal.abnormal_1);
            return fictionContentVO;
        }

        FictionContent fictionContents = lambdaQuery().eq(FictionContent::getFictionChapterId,
                fictionChapter.getFictionChapterId()).one();

        // 添加章节信息
        fictionContentVO.setFictionChapter(fictionChapter);

        // 小说内容不为空则返回数据
        if (fictionContents != null){
            fictionContentVO.setContents(JSONObject.parseObject(fictionContents.getContent(), List.class));
            fictionContentVO.setFictionContent(fictionContents);
            return fictionContentVO;
        }

        String key = RedisFinal.FICTION_CONTENT_KEY + fictionChapter.getFictionChapterId();

        try {
            // 尝试获取锁
            boolean isLock = redisUtil.tryLock(key);

            // 获取锁失败，等待数据库更新，并返回数据
            if (!isLock){
                Thread.sleep(200);
                return getFictionContent(fictionChapter);
            }

            // 解析小说内容
            fictionContents = DocumentToObject(JsoupBooksUtil.getDoc(SearchFinal.BOOKS +
                            fictionChapter.getChapterLink()),
                    fictionChapter.getFictionChapterId());

            // 保存小说内容
            save(fictionContents);

            fictionContentVO.setContents(
                    JSONObject.parseObject(fictionContents.getContent(), List.class));
            fictionContentVO.setFictionContent(fictionContents);

        }catch (Exception e){
            e.printStackTrace();
            Code.BUSINESS_ERR("解析或保存小说内容失败");
        }finally {
            // 释放锁
            redisUtil.releaseLock(key);
        }

        return fictionContentVO;
    }

    @Override
    public FictionContent DocumentToObject(Document document, int id) {
        return fictionDocument.DocumentToObjectFictionContent(document, id);
    }
}
